package cn.outsourcing.supermarket.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Date;

/**
 * @author gzkemays
 * @since 2021/10/20 13:18
 */
@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = {"cn.outsourcing.supermarket.mapper"})
public class MybatisPlusConfig implements MetaObjectHandler {
  /**
   * MP 分页插件
   *
   * @return {@link PaginationInterceptor}
   */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    System.out.println("分页初始化");
    return new PaginationInterceptor();
  }

  /**
   * 定义 insert 操作时指定字段的默认数据
   *
   * @param metaObject 元对象
   */
  @Override
  public void insertFill(MetaObject metaObject) {
    this.setFieldValByName("createTime", new Date(), metaObject);
    this.setFieldValByName("updateTime", new Date(), metaObject);
  }

  /**
   * 定义 update 操作时指定字段的默认数据
   *
   * @param metaObject 元对象
   */
  @Override
  public void updateFill(MetaObject metaObject) {
    this.setFieldValByName("updateTime", new Date(), metaObject);
  }

}
